package com.ruoyi.ruoyiserver.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ruoyiserver.domain.PeBackup;
import com.ruoyi.ruoyiserver.service.IPeBackupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/person/backup")
public class PeBackupController extends BaseController {
    @Autowired
    private IPeBackupService peBackupService;

    /**
     * 查询【请填写功能名称】列表
     */
    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:list')")
    @GetMapping("/list")
    public TableDataInfo list(PeBackup peBackup) {
        startPage();
        List<PeBackup> list = peBackupService.selectPeBackupList(peBackup);
        return getDataTable(list);
    }

//    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, PeBackup peBackup) {
//        List<PeBackup> list = peBackupService.selectPeBackupList(peBackup);
//        ExcelUtil<PeBackup> util = new ExcelUtil<PeBackup>(PeBackup.class);
//        util.exportExcel(response, list, "【请填写功能名称】数据");
//    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(peBackupService.selectPeBackupById(id));
    }
//
//    /**
//     * 新增【请填写功能名称】
//     */
//    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:add')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody PeBackup peBackup) {
//        return toAjax(peBackupService.insertPeBackup(peBackup));
//    }

    /**
     * 修改【请填写功能名称】
     */
    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:edit')")
    @Log(title = "【编辑】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PeBackup peBackup) {
        return toAjax(peBackupService.updatePeBackup(peBackup));
    }

    /**
     * 删除【请填写功能名称】
     */
//    // @PreAuthorize("@ss.hasPermi('ruoyiserver:backup:remove')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable String[] ids) {
//        return toAjax(peBackupService.deletePeBackupByIds(ids));
//    }

    @ApiOperation("手动触发备份")
    @GetMapping("back-sql")
    public AjaxResult backSql() {
        peBackupService.beginBackSql("手动备份");
        return success();
    }

    @ApiOperation("恢复数据库")
    @GetMapping("/recover/{id}")
    public AjaxResult recover(@PathVariable("id") String id) {
        peBackupService.recover(id);
        return success();
    }

    @ApiOperation("查询数据库恢复状态")
    @GetMapping("get-recover-status")
    public AjaxResult getRecoverStatus(){
        // false 备份完成，true 备份中
        Boolean backStatus = peBackupService.getRecoverStatus();
        return AjaxResult.success(backStatus);
    }
}
